package com.yz.crm.phase.action;

import com.mchange.lang.DoubleUtils;
import com.yz.common.model.Pager;
import com.yz.common.model.RemoteInteractObject;
import com.yz.crm.db.model.BasePhase;
import com.yz.crm.db.model.BasePhaseItem;
import com.yz.crm.passport.model.SessionUser;
import com.yz.crm.phase.constant.PhaseConstant;
import com.yz.crm.phase.service.PhaseService;
import com.yz.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * Created by DOVE on 2017/5/18 0018.
 */
@Controller
@RequestMapping("/admin/phase/phaseItem")
public class PhaseItemAction {
    private static final Log log = LogFactory.getLog(PhaseItemAction.class);

    @Resource
    private PhaseService phaseService;

    @RequestMapping("/index")
    public String index() {
        return "/admin/phase/index";
    }

    @RequestMapping("/list")
    public String list(Pager pager, ModelMap modelMap, HttpServletRequest request) {
        SessionUser user = WebUtils.getLoginUser(request);
        if (user == null) {
            return null;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(PhaseConstant.PAGER_KEY_COMPANY_ID, user.getCompany().getId());
        pager.setKeys(paramMap);
        modelMap.addAttribute("pager", phaseService.findPager(pager));
        return "/admin/phase/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, String id, ModelMap modelMap) {
        BasePhase phase = phaseService.getById(id);
        List<BasePhaseItem> phaseItemList = phaseService.findListByPhaseId(id);
        modelMap.addAttribute("phase", phase);
        modelMap.addAttribute("phaseItemList", phaseItemList);
        return "/admin/phase/phaseItem/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public RemoteInteractObject save(HttpServletRequest request, String phaseId) {
        RemoteInteractObject result = new RemoteInteractObject();
        SessionUser user = WebUtils.getLoginUser(request);
        if (user == null) {
            result.setSuccess(false);
            result.setMsg("查询公司信息出错，请联系管理员");
            return result;
        }
        List<BasePhaseItem> list = getItemList(request, phaseId);
        phaseService.addPhaseItem(list);
        result.setSuccess(true);
        return result;
    }

    private List<BasePhaseItem> getItemList(HttpServletRequest request, String phaseId) {
        List<BasePhaseItem> list = new ArrayList<BasePhaseItem>();
        String[] names = request.getParameterValues("name");
        String[] descriptions = request.getParameterValues("description");
        String[] weightValues = request.getParameterValues("weightValue");
        String[] orderNums = request.getParameterValues("orderNum");
        if (names == null) {
            return list;
        }
        BasePhaseItem item;
        for (int i = 0; i < names.length; i++) {
            item = new BasePhaseItem();
            item.setPhaseId(phaseId);
            item.setName(names[i]);
            item.setDescription(descriptions[i]);
            if (StringUtils.isNotEmpty(weightValues[i])) {
                item.setWeightValue(Double.valueOf(weightValues[i]));
            }
            if (StringUtils.isNotEmpty(orderNums[i])) {
                item.setOrderNum(Integer.valueOf(orderNums[i]));
            }
            list.add(item);
        }
        return list;
    }
}
